package com.ang.juc.n4.t2;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;
import java.util.stream.IntStream;

/**
 * park,unpark
 */
@Slf4j
public class Test3 {

    static Thread ta;
    static Thread tb;
    static Thread tc;

    public static void main(String[] args) {
        IntStream.range(0, 3).forEach(i -> {
            Thread ta = new Thread(new Test1.PrintA(), "ta" + i);
            Thread tb = new Thread(new Test1.PrintB(), "tb" + i);
            Thread tc = new Thread(new Test1.PrintC(), "ta" + i);

            ta.start();
            tb.start();
            tc.start();
        });
    }

    static class printA implements Runnable {
        @Override
        public void run() {
            LockSupport.park();
            IntStream.range(0, 5).forEach(i -> {
                log.info("a");
            });

            //唤醒b
            LockSupport.unpark(tb);
        }
    }

    static class printB implements Runnable {
        @Override
        public void run() {
            LockSupport.park();
            IntStream.range(0, 5).forEach(i -> {
                log.info("b");
            });

            //唤醒c
            LockSupport.unpark(tc);
        }
    }

    static class printC implements Runnable {
        @Override
        public void run() {
            LockSupport.park();
            IntStream.range(0, 5).forEach(i -> {
                log.info("c");
            });

            //唤醒c
            LockSupport.unpark(ta);
        }
    }
}
